package com.jarvis.app.car.fragment;

import android.accounts.Account;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.jarvis.app.car.AboutActivity;
import com.jarvis.app.car.AnswerTicketActivity;
import com.jarvis.app.car.LoginActivity;
import com.jarvis.app.car.R;
import com.jarvis.app.car.RegisterActivity;
import com.jarvis.app.car.TicketActivity;
import com.jarvis.app.car.UpdateNameActivity;
import com.jarvis.app.car.UpdatePwdActivity;
import com.jarvis.app.car.constant.AppConstant;

/**
 * create an instance of this fragment.
 */
public class MineFragment extends Fragment {

    private View view;

    private TextView tv_userName;

    private TextView tv_phone;

    private TextView tv_exit;

    private TextView tv_bolSuper;

    private RelativeLayout rl_updatePwd;

    private RelativeLayout rl_about;

    private RelativeLayout rl_updateName;

    private RelativeLayout rl_answerTicket;

    private RelativeLayout rl_ticket;

    private ImageView iv_img;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine, container, false);

        tv_userName = view.findViewById(R.id.tv_userName);
        tv_phone = view.findViewById(R.id.tv_phone);
        tv_exit = view.findViewById(R.id.tv_exit);
        tv_bolSuper = view.findViewById(R.id.tv_bolSuper);
        rl_updatePwd = view.findViewById(R.id.rl_updatePwd);
        rl_about = view.findViewById(R.id.rl_about);
        rl_updateName = view.findViewById(R.id.rl_updateName);
        rl_ticket = view.findViewById(R.id.rl_ticket);
        rl_answerTicket = view.findViewById(R.id.rl_answerTicket);
        iv_img = view.findViewById(R.id.iv_img);

        if (AppConstant.ACCOUNT_SUPER != 1) {
            rl_ticket.setVisibility(View.VISIBLE);
            //超级管理员
            rl_ticket.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), AnswerTicketActivity.class);
                    startActivity(intent);
                }
            });
        } else {
            rl_answerTicket.setVisibility(View.VISIBLE);
            rl_answerTicket.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), AnswerTicketActivity.class);
                    startActivity(intent);
                }
            });
        }

        tv_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(getContext())
                        .setTitle("温馨提示！")
                        .setMessage("确定要退出吗？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                getActivity().finish();

                                Intent intent = new Intent(getActivity(), LoginActivity.class);
                                startActivity(intent);
                            }
                        })
                        .show();
            }
        });
        rl_updatePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UpdatePwdActivity.class);
                startActivityForResult(intent, Integer.MAX_VALUE);
            }
        });

        rl_about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AboutActivity.class);
                startActivity(intent);
            }
        });
        rl_updateName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UpdateNameActivity.class);
                startActivityForResult(intent, Integer.MIN_VALUE);
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        tv_userName.setText(AppConstant.ACCOUNT_NAME);
        tv_phone.setText(AppConstant.ACCOUNT_PHONE);

        Glide.with(getActivity())
                .load(AppConstant.ACCOUNT_IMG)
                .into(iv_img);

        tv_bolSuper.setText(AppConstant.ACCOUNT_SUPER == 1 ? "超级管理员" : "普通账户");

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Integer.MAX_VALUE) {
            getActivity().finish();

            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        } else if (resultCode == Integer.MIN_VALUE) {
            tv_userName.setText(AppConstant.ACCOUNT_NAME);
        }
    }


}